<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable" @edit-change="handleChange({ column, value, record })">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <!-- <a-button type="primary" @click="handleForm({})" v-auth="'shop:other:set:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button> -->
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.id }}
        </a>
      </template>
      <template #picUrl="{ record }">
        <a-image
          :width="50"
          :src="record.picUrl ? ctxPath + record.picUrl : '/resource/img/noSet.png'"
        />
      </template>
      <template v-if="hasPermission(['shop:other:set:view'])" #uploadFile="{ record }">
        <BasicUpload
          v-model:value="record.dataMap"
          :bizKey="record.id"
          :bizType="'shopOther_set_image'"
          :uploadType="'image'"
          :maxNumber="2"
          :maxSize="1"
          :loadTime="record.__t"
          :showPreview="true"
          :emptyHidePreview="true"
          @change="selPicChange($event, record.id)"
        />
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsShopOtherShopOtherSetList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import {
    shopOtherSetDelete,
    shopOtherSetListData,
    savePic,
  } from '/@/api/shop/other/shopOtherSet';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import { usePermission } from '/@/hooks/web/usePermission';
  import { BasicUpload } from '/@/components/Upload';
  import { useGlobSetting } from '/@/hooks/setting';

  const { t } = useI18n('shop.other.shopOtherSet');
  const { hasPermission } = usePermission();
  const { ctxPath } = useGlobSetting();

  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('商城其它设置管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('类型'),
        field: 'stype',
        component: 'Select',
        componentProps: {
          dictType: 'shop_other_set',
          allowClear: true,
        },
      },
      {
        label: t('备注信息'),
        field: 'remarks',
        component: 'Input',
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('ID'),
      dataIndex: 'id',
      key: 'a.id',
      sorter: true,
      width: 120,
      align: 'left',
      slot: 'firstColumn',
      ifShow: false,
    },
    {
      title: t('上传'),
      dataIndex: 'uploadFile',
      key: 'fileId',
      sorter: false,
      width: 100,
      align: 'left',
      slot: 'uploadFile',
    },
    {
      title: t('缩略图'),
      dataIndex: 'picUrl',
      key: 'a.pic_url',
      sorter: false,
      width: 100,
      align: 'center',
      slot: 'picUrl',
    },
    {
      title: t('类型'),
      dataIndex: 'stype',
      key: 'a.stype',
      sorter: true,
      width: 100,
      align: 'left',
      dictType: 'shop_other_set',
      //editable: true,
      // edit: true,
      // editComponent: 'Select',
      // editComponentProps: {
      //   dictType: 'shop_other_set',
      // },
    },
    {
      title: t('图片地址'),
      dataIndex: 'picUrl',
      key: 'a.pic_url',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('备注信息'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 130,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 100,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑商城其它设置'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'shop:other:set:edit',
      },
      // {
      //   icon: 'ant-design:delete-outlined',
      //   color: 'error',
      //   title: t('删除商城其它设置'),
      //   popConfirm: {
      //     title: t('是否确认删除商城其它设置'),
      //     confirm: handleDelete.bind(this, { id: record.id }),
      //   },
      //   auth: 'shop:other:set:edit',
      // },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm }] = useTable({
    api: shopOtherSetListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    //actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await shopOtherSetDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  async function selPicChange(fileList, id) {
    await savePic({ id, dataMap: fileList });
    reload();
  }

  function handleChange({ column, value, record }) {
    console.log(column, value, record);
  }

  function handleSuccess() {
    reload();
  }
</script>
<style>
  .jeesite-editable-cell__action {
    display: none;
  }
  /* .ant-select-show-search {
    width: 100% !important ;
  } */
  .edit-cell-align-center {
    width: 100% !important;
  }
</style>
